using UnityEngine;
using System.Collections;

public class BasicBullet : MonoBehaviour
{
	public float	m_fSpeed				=	10;	
	public bool		m_bDestroyIfNotVisible 	= 	false;
	public float	m_fTimeCoef;	
	
	
	//---------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------------------------
	public void ChangeTimeCoef(float _fNewTimeCoef)
	{
		m_fTimeCoef *= _fNewTimeCoef;
	}
	
	void Start()
	{
		m_fTimeCoef	=	1.0f;
		if (m_bDestroyIfNotVisible)
		{
			StartCoroutine("CheckDestruction");
		}
		
		// Init the external data controller
		ExternalTimeController	etmc_Controller = gameObject.GetComponent("ExternalTimeController") as ExternalTimeController; 
		etmc_Controller.SetDelegateToChangeTimeCoef(new PassTimeCoef(ChangeTimeCoef));
		
		// Attach to gameplay area
		transform.parent = GameObject.Find( "GameplayArea" ).transform;
	}
	
	// Update is called once per frame
	void Update ()
	{
		transform.position = transform.position + transform.up * m_fSpeed * Time.deltaTime * m_fTimeCoef;
		//transform.Translate(transform.up * m_fSpeed * Time.deltaTime, Space.World);
	}
	
	
	public void SetForDestructionOnBecameInvisible()
	{
		if (!m_bDestroyIfNotVisible)
		{
			StartCoroutine("CheckDestruction");
			m_bDestroyIfNotVisible = true;
		}
	}
	
	
	void OnBecameInvisible()
	{
		if(m_bDestroyIfNotVisible)
		{
			Destroy(gameObject);
		}
	}
	
	IEnumerator CheckDestruction()
	{
		yield return new WaitForSeconds(1);
		
		if(!renderer.isVisible)
		{
			Destroy(gameObject);
		}
		else
		{
			yield return StartCoroutine("CheckDestruction");
		}
	}
}

